package fms.controller;

import fms.core.IFilter;
import fms.dao.CptSendService;
import fms.model.CptSend;
import fms.msgupcenter.protocols.AcarsProtocol;
import fms.msgupcenter.protocols.uplinkmsg.WindMsgProtocol;
import fms.util.ConfUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *      1、FlightOperator不直接处理飞机or机长回复的“风温/飞行计划”；
 *      2、MsgUpCenter会接收到所有下行报文，对“风温/飞行计划回复报文”进行处理，并以XML协议格式发送到activeMQ中；
 *      3、FlightOperator收到对“风温/飞行计划回复报文”的处理结果后，可以获取到原始回复报文发送时生成的ID，用该ID找到原始发送记录，并更新发送状态，以便在web中显示；
 */
public class WindAcarsController implements IReceiver {
//public class WindAcarsController implements Observer {

    private Logger logger = Logger.getLogger(WindAcarsController.class);
    ExecutorService pool = Executors.newFixedThreadPool(10);

    private IFilter filter;

    public WindAcarsController() {

    }


//    public void update(Observable observable, Object arg) {
//        String msg = (String) arg;
//        if (filter.isFilter(msg))
//        {
//            DealWindAcars dealWindAcarsRequest = new DealWindAcars(msg);
//            pool.execute(dealWindAcarsRequest);
//        }else{
//            return;
//        }
//    }

    public void receiveMsg(Object arg) {
        String msg = (String) arg;
        if (filter.isFilter(msg))
        {
            DealWindAcars dealWindAcarsRequest = new DealWindAcars(msg);
            pool.execute(dealWindAcarsRequest);
        }else{
            return;
        }
    }

    private static class DealWindAcars implements Runnable {
        private Logger logger = Logger.getLogger(DealWindAcars.class);
        private String msg;

        public void dealWindAcars( String msg) {
            try {
                logger.info("[开始]飞机下发的(风温/飞行计划)回复信息(AC/AK)处理");

                WindMsgProtocol protocol = (WindMsgProtocol) AcarsProtocol.Deserialize(
                        msg, WindMsgProtocol.class);
                String appUpMsgId = protocol.body.windMessage.getHead().getAppUpMsgId();
                String status = protocol.body.windMessage.getHead().getState();


                CptSendService cptSendService = ConfUtil.getSingletonApplicationContextDao().getBean("cptSendService", CptSendService.class);
                List listsend = cptSendService.findCptSendbymsgId(appUpMsgId);
                if(listsend != null && listsend.size() > 0){
                    CptSend cptSend = (CptSend)listsend.get(0);
                    if("9".equals(status)) {
                        cptSend.setSendStatus("REJ");
                    }else  if("8".equals(status)){
                        cptSend.setSendStatus("AK");
                    } else  if("7".equals(status)) {
                        cptSend.setCaptainStatus("AC");
                    }
                    cptSendService.saveOrUpdate(cptSend);
                }
                logger.debug("[结束]飞机下发的(风温/飞行计划)回复信息(AC/AK)处理\r\n" + msg);
            } catch (Exception ex) {
                logger.error("[异常]飞机下发的(风温/飞行计划)回复信息(AC/AK)处理：" + ex.toString());
            }
        }

        private DealWindAcars(String msg ) {
            this.msg = msg;          }

        @Override
        public void run() {
            dealWindAcars(msg);
        }
    }

    public IFilter getFilter() {
        return filter;
    }

    public void setFilter(IFilter filter) {
        this.filter = filter;
    }
}
